﻿@model ZDSoft.LMS.Domain.User
@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
    
    var roles = ViewBag.Roles as IList<ZDSoft.LMS.Domain.Role>;
}
<link href="@Url.Content("~/Content/Style/ViewPage.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<table border="0" cellpadding="1" cellspacing="0" style="width: 100%; background-color: White;"
    align="center">
    <tr>
        <td class="mbg">
            <table cellspacing="0" cellpadding="0" width="100%" border="0" valign="top">
                <tr>
                    <td>
                        <table style="height: 23px;" cellspacing="0" cellpadding="0" width="120" background="@Url.Content("~/Content/Images/m_17.gif")"
                            border="0">
                            <tr>
                                <td>
                                    <span class="contentPageTitle">&nbsp;&gt;&gt; 创建用户</span>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="titleContentDivider" height="6">
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<p>
    @Html.ActionLink("返回列表", "Index")
</p>

<script language="javascript" type="text/javascript">
    //全选与反选
    function CheckAll() {
       // $("#chkAll").attr("checked") == true ? $("#tbRoles").find("input").attr("checked", true) : $("#tbRoles").find("input").attr("checked", false);

        var selectedIds = ",";
        if ($("#chkAll").attr("checked") == true) {
            $("#tbRoles").find("input").each(function () {
                //this.checked = "checked";
                $(this).attr("checked", true);
            });
            selectedIds = GetAllIds();
        }
        else {
            $("#tbRoles").find("input").each(function () {
                this.checked = false;
            });
        }
        $("#hdSelectedIds").val(selectedIds);
    }

    //获取所有的角色ID
    function GetAllIds() {
        var ids = ",";
        $("#tbRoles").find("input").each(function () {
            if (this.id != "chkAll") {
                ids += "," + this.id + ",";
            }
        });
        return ids;
    }

    //选择角色ID
    function SetSelectedId(ctl, id) {
        var val = "";
        if (ctl.checked) {
            val = $("#hdSelectedIds").val();
            if (val.indexOf("," + id + ",") < 0) {
                val += "," + id + ",";
            }
        }
        else {
            val = $("#hdSelectedIds").val();
            if (val.indexOf("," + id + ",") > 0) {
                val = val.replace("," + id + ",", "");
            }
        }
        $("#hdSelectedIds").val(val);
    }

    //提交之前选择角色
    function CheckSelectedRoles() {
        if ($("#hdSelectedIds").val() == ",") {
            if (confirm("不打算配置角色吗?")) {
                return true;
            }
            return false;
        }
    }
</script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>创建用户</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Account)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Account) 
            @Html.ValidationMessageFor(model => model.Account)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
            (系统初始)
        </div>
        <div class="editor-field">
            <input type="password" disabled="disabled" value="123" />
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.IsActive)
        </div>
        <div class="editor-field">
            @Html.CheckBoxFor(model => model.IsActive, new { Checked = "checked" })
            @Html.ValidationMessageFor(model => model.IsActive)
        </div>
    </fieldset>
    <div style="height:10px;"></div>
    <fieldset>
        <legend>配置角色</legend>
        <table cellpadding="0" cellspacing="0" class="dataGrid" id="tbRoles">
            <!-- 列头开始 -->
            <tr>                
                <th>
                    <input type="checkbox" id="chkAll" onclick="CheckAll()" />全选/取消
                </th>
                <th>
                    角色名称
                </th>
            </tr>
            <!-- 列头结束 -->

            <!-- 动态生成的行开始 -->
            @if (roles == null || roles.Count == 0)
            {
                <tr>
                    <td colspan="2">
                        <span>没有满足条件的记录!</span>
                    </td>
                </tr>
            }
            else
            {
                foreach (var item in roles)
                {
                <tr>
                    <td>
                        <input type="checkbox" id="@item.ID" onclick="SetSelectedId(this,'@item.ID')" />
                    </td>
                    <td>
                        @item.RoleName
                    </td>
                </tr>
                }
            }
            <!-- 动态生成的行结束 -->
        </table>
        <input type="hidden" id="hdSelectedIds" name="hdSelectedIds" value="," />
        <p>
            <input type="submit" value="保存" class="btn3" onclick="return CheckSelectedRoles()" />
        </p>
    </fieldset>
}